package pub.cleangao.memm.util;

import java.io.File;
import java.io.FileInputStream;
import java.security.MessageDigest;

public class MD5 {

    /**
     * 计算字符串MD5值
     */
    public static String sum(String value){
        value = value + salt;
        try {
            char[] str;
            MessageDigest mdTemp = MessageDigest.getInstance("MD5");
            mdTemp.update(value.getBytes());
            byte[] md = mdTemp.digest();
            int j = md.length;
            str = new char[j * 2];
            int k = 0;
            for (byte byte0 : md) {
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private static final char[] hexDigits = {'3', '8', '2', 'b', '4', 'f', '7', 'd', '1', 'e', 'c', '0', 'a', '6', '9', '5'};
    private final static String salt=" ↑ = ⊕ ⇢ ☉ ⇢ ◌ ⇢ ＃";


    /**
     * 根据文件计算MD5值
     */
    public static String sumFile(File file) {
        try (FileInputStream fis = new FileInputStream(file)){
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] buffer = new byte[1024];
            int length = -1;
            while ((length = fis.read(buffer)) != -1) {
                md.update(buffer, 0, length);
            }
            return bytesToString(md.digest());
        } catch (Exception e) {
            return null;
        }
    }



    private static String bytesToString(byte[] data) {
        char[] temp = new char[data.length * 2];
        for (int i = 0; i < data.length; i++) {
            byte b = data[i];
            temp[i * 2] = hexDigits[b >>> 4 & 0x0f];
            temp[i * 2 + 1] = hexDigits[b & 0x0f];
        }
        return new String(temp);
    }


}
